home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 8: LINUX Games
/
Linux Cubed Series 8 - LINUX Games.iso
/
games
/
x11
/
strategy
/
xpuzzles.3
/
xpuzzles
/
xpuzzles-5.3.1
/
xhexagons
/
xmhexagons.c
< prev
Wrap
C/C++ Source or Header
|
1996-02-07
|
13KB
|
442 lines
/*
# MOTIF-BASED HEXAGONS
#
# xmhexagons.c
#
###
#
# Copyright (c) 1993 - 96 David Albert Bagley, bagleyd@hertz.njit.edu
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of the author not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# This program is distributed in the hope that it will be "useful",
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
*/
/*
Version 5: 95/10/01 Xt/Motif
Version 4: 94/06/07 Xt
Version 3: 93/04/01 Motif
Version 2: 92/01/07 XView
Version 1: 91/09/05 SunView
*/
#include <stdlib.h>
#include <stdio.h>
#ifdef VMS
#include <unixlib.h>
#define getlogin cuserid
#else
#ifndef apollo
#include <unistd.h>
#endif
#endif
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/cursorfont.h>
#include <Xm/PanedW.h>
#include <Xm/RowColumn.h>
#include <Xm/Label.h>
#include <Xm/LabelG.h>
#include <Xm/Scale.h>
#include <Xm/ToggleB.h>
#include "Hexagons.h"
#include "hexagons.xbm"
#include "mouse-l.xbm"
#include "mouse-r.xbm"
#ifndef SCOREFILE
#define SCOREFILE "/usr/games/lib/hexagons.scores"
#endif
/* The following are in HexagonsP.h also */
#define MINHEXAGONS 1
#define MAXORIENT 2
#define NOCORN 0
#define CORNERS 1
#define MAXHEXAGONS 12
#define MAXRECORD 32767
static void Initialize();
static void CallbackHexagons();
static void PrintRecord();
static int HandleSolved();
static void ReadRecords();
static void WriteRecords();
static void motif_printf();
static void HexSlider();
static void CornersToggle();
static Arg arg[2];
static Widget moves, record, message, hexagons, cornersSwitch, hex;
static int hexagonsRecord[MAXORIENT][MAXHEXAGONS - MINHEXAGONS + 1],
movesDsp = 0;
static char messageDsp[128] = "Welcome";
static void Usage()
{
(void) fprintf(stderr, "usage: xmhexagons\n");
(void) fprintf(stderr,
"\t[-geometry [{width}][x{height}][{+-}{xoff}[{+-}{yoff}]]]\n");
(void) fprintf(stderr,
"\t[-display [{host}]:[{vs}]][-fg {color}] [-bg {color}]\n");
(void) fprintf(stderr,
"\t[-tile {color}] [-{border|bd} {color}] [-size {int}] [-[no]corners]\n");
exit(1);
}
static XrmOptionDescRec options[] = {
{"-fg", "*hexagons.Foreground", XrmoptionSepArg, NULL},
{"-bg", "*Background", XrmoptionSepArg, NULL},
{"-foreground", "*hexagons.Foreground", XrmoptionSepArg, NULL},
{"-background", "*Background", XrmoptionSepArg, NULL},
{"-tile", "*hexagons.tileColor", XrmoptionSepArg, NULL},
{"-border", "*hexagons.tileBorder", XrmoptionSepArg, NULL},
{"-bd", "*hexagons.tileBorder", XrmoptionSepArg, NULL},
{"-size", "*hexagons.size", XrmoptionSepArg, NULL},
{"-corners", "*hexagons.corners", XrmoptionNoArg, "TRUE"},
{"-nocorners", "*hexagons.corners", XrmoptionNoArg, "FALSE"}
};
int main(argc, argv)
int argc;
char *argv[];
{
Widget toplevel;
Widget panel, panel2, rowcol, rowcol2;
Pixmap mouseLeftCursor, mouseRightCursor;
Pixel fg, bg;
toplevel = XtInitialize(argv[0], "Hexagons",
options, XtNumber(options), &argc, argv);
if (argc != 1)
Usage();
XtSetArg(arg[0], XtNiconPixmap,
XCreateBitmapFromData(XtDisplay(toplevel),
RootWindowOfScreen(XtScreen(toplevel)),
(char *) hexagons_bits, hexagons_width, hexagons_height));
XtSetArg(arg[1], XmNkeyboardFocusPolicy, XmPOINTER); /* not XmEXPLICIT */
XtSetValues(toplevel, arg, 2);
panel = XtCreateManagedWidget("panel", xmPanedWindowWidgetClass, toplevel,
NULL, 0);
panel2 = XtVaCreateManagedWidget("panel2", xmPanedWindowWidgetClass, panel,
XmNseparatorOn, False,
XmNsashWidth, 1,
XmNsashHeight, 1,
NULL);
rowcol = XtVaCreateManagedWidget("Rowcol", xmRowColumnWidgetClass, panel2,
XmNnumColumns, 2,
XmNorientation, XmHORIZONTAL,
XmNpacking, XmPACK_COLUMN,
NULL);
XtVaGetValues(rowcol, XmNforeground, &fg, XmNbackground, &bg, NULL);
mouseLeftCursor = XCreatePixmapFromBitmapData(XtDisplay(rowcol),
RootWindowOfScreen(XtScreen(rowcol)), mouse_left_bits,
mouse_left_width, mouse_left_height, fg, bg,
DefaultDepthOfScreen(XtScreen(rowcol)));
mouseRightCursor = XCreatePixmapFromBitmapData(XtDisplay(rowcol),
RootWindowOfScreen(XtScreen(rowcol)), mouse_right_bits,
mouse_right_width, mouse_right_height, fg, bg,
DefaultDepthOfScreen(XtScreen(rowcol)));
XtVaCreateManagedWidget("mouseLeftText", xmLabelGadgetClass, rowcol,
XtVaTypedArg, XmNlabelString, XmRString, "Move tile", 10, NULL);
XtVaCreateManagedWidget("mouseLeft", xmLabelGadgetClass, rowcol,
XmNlabelType, XmPIXMAP, XmNlabelPixmap, mouseLeftCursor, NULL);
XtVaCreateManagedWidget("mouseRightText", xmLabelGadgetClass, rowcol,
XtVaTypedArg, XmNlabelString, XmRString, "Randomize", 10, NULL);
XtVaCreateManagedWidget("mouseRight", xmLabelGadgetClass, rowcol,
XmNlabelType, XmPIXMAP, XmNlabelPixmap, mouseRightCursor, NULL);
XtVaCreateManagedWidget("movesText", xmLabelGadgetClass, rowcol,
XtVaTypedArg, XmNlabelString, XmRString, "Moves", 6, NULL);
moves = XtVaCreateManagedWidget("0", xmLabelWidgetClass, rowcol, NULL);
XtVaCreateManagedWidget("recordText", xmLabelGadgetClass, rowcol,
XtVaTypedArg, XmNlabelString, XmRString, "Record", 7, NULL);
record = XtVaCreateManagedWidget("0", xmLabelWidgetClass, rowcol, NULL);
rowcol2 = XtVaCreateManagedWidget("Rowcol2", xmRowColumnWidgetClass, panel2,
NULL);
XtVaGetValues(rowcol2, XmNforeground, &fg, XmNbackground, &bg, NULL);
hex = XtVaCreateManagedWidget("hexs", xmScaleWidgetClass, rowcol2,
XtVaTypedArg, XmNtitleString, XmRString, "Hexs", 5,
XmNminimum, MINHEXAGONS,
XmNmaximum, MAXHEXAGONS,
XmNvalue, MINHEXAGONS,
XmNshowValue, True,
XmNorientation, XmHORIZONTAL,
NULL);
XtAddCallback(hex, XmNvalueChangedCallback, HexSlider, NULL);
cornersSwitch = XtVaCreateManagedWidget ("Corners",
xmToggleButtonWidgetClass, rowcol2,
NULL);
XtAddCallback(cornersSwitch, XmNvalueChangedCallback, CornersToggle, NULL);
message = XtVaCreateManagedWidget("Play Hexagons! (use mouse or keypad)",
xmLabelWidgetClass, rowcol2,
NULL);
hexagons = XtCreateManagedWidget("hexagons", hexagonsWidgetClass, panel,
NULL, 0);
XtAddCallback(hexagons, XtNselectCallback, CallbackHexagons, NULL);
Initialize(hexagons);
XtRealizeWidget(toplevel);
XGrabButton(XtDisplay(hexagons), AnyButton, AnyModifier, XtWindow(hexagons),
TRUE, ButtonPressMask | ButtonMotionMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, XtWindow(hexagons),
XCreateFontCursor(XtDisplay(hexagons), XC_crosshair));
XtMainLoop();
#ifdef VMS
return 1;
#else
return 0;
#endif
}
static void Initialize(w)
Widget w;
{
int size;
Boolean corners;
XtVaSetValues(w,
XtNstart, FALSE,
NULL);
XtVaGetValues(w,
XtNsize, &size,
XtNcorners, &corners,
NULL);
if (size <= MAXHEXAGONS)
XmScaleSetValue(hex, size);
XmToggleButtonSetState(cornersSwitch, corners, True);
ReadRecords();
PrintRecord(size, corners);
}
static void CallbackHexagons(w, clientData, callData)
Widget w;
caddr_t clientData;
hexagonsCallbackStruct *callData;
{
int size;
Boolean corners;
XtVaGetValues(w,
XtNsize, &size,
XtNcorners, &corners,
NULL);
(void) strcpy(messageDsp, "");
switch (callData->reason) {
case HEXAGONS_RESTORE:
case HEXAGONS_RESET:
movesDsp = 0;
break;
case HEXAGONS_BLOCKED:
(void) strcpy(messageDsp, "Blocked");
break;
case HEXAGONS_SPACE:
/*(void) strcpy(messageDsp, "Spaces can't move");*/ /* Too annoying */
break;
case HEXAGONS_IGNORE:
(void) strcpy(messageDsp, "Randomize to start");
break;
case HEXAGONS_MOVED:
movesDsp++;
XtSetArg(arg[0], XtNstart, TRUE);
XtSetValues(w, arg, 1);
break;
case HEXAGONS_SOLVED:
if (HandleSolved(movesDsp, size, corners))
(void) sprintf(messageDsp, "Congratulations %s!!", getlogin());
else
(void) strcpy(messageDsp, "Solved!");
XtSetArg(arg[0], XtNstart, FALSE);
XtSetValues(w, arg, 1);
break;
case HEXAGONS_RANDOMIZE:
movesDsp = 0;
XtSetArg(arg[0], XtNstart, FALSE);
XtSetValues(w, arg, 1);
break;
case HEXAGONS_DEC:
movesDsp = 0;
size--;
PrintRecord(size, corners);
XtSetArg(arg[0], XtNsize, size);
XtSetValues(w, arg, 1);
if (size <= MAXHEXAGONS)
XmScaleSetValue(hex, size);
break;
case HEXAGONS_INC:
movesDsp = 0;
size++;
PrintRecord(size, corners);
XtSetArg(arg[0], XtNsize, size);
XtSetValues(w, arg, 1);
if (size <= MAXHEXAGONS)
XmScaleSetValue(hex, size);
break;
case HEXAGONS_CORNERS:
movesDsp = 0;
corners = !corners;
PrintRecord(size, corners);
XtSetArg(arg[0], XtNcorners, corners);
XtSetValues(w, arg, 1);
XmToggleButtonSetState(cornersSwitch, corners, True);
break;
case HEXAGONS_COMPUTED:
XtSetArg(arg[0], XtNstart, FALSE);
XtSetValues(w, arg, 1);
break;
case HEXAGONS_UNDO:
movesDsp--;
XtSetArg(arg[0], XtNstart, TRUE);
XtSetValues(w, arg, 1);
break;
}
motif_printf(message, "%s", messageDsp);
motif_printf(moves, "%d", movesDsp);
}
static void HexSlider(w, clientData, cbs)
Widget w;
XtPointer clientData;
XmScaleCallbackStruct *cbs;
{
int size = cbs->value, old;
XtVaGetValues(hexagons,
XtNsize, &old,
NULL);
if (old != size) {
XtVaSetValues(hexagons,
XtNsize, size,
NULL);
movesDsp = 0;
motif_printf(moves, "%d", movesDsp);
PrintRecord(size, XmToggleButtonGetState(cornersSwitch));
}
}
static void CornersToggle(w, clientData, cbs)
Widget w;
XtPointer clientData;
XmToggleButtonCallbackStruct *cbs;
{
int size;
Boolean corners = cbs->set;
XtVaSetValues(hexagons,
XtNcorners, corners,
NULL);
XtVaGetValues(hexagons,
XtNsize, &size,
NULL);
movesDsp = 0;
motif_printf(moves, "%d", movesDsp);
PrintRecord(size, corners);
}
static void PrintRecord(size, corners)
int size;
Boolean corners;
{
int i = (corners) ? 1 : 0, j = size - MINHEXAGONS;
if (size > MAXHEXAGONS)
motif_printf(record, "NOT RECORDED");
if (hexagonsRecord[i][j] >= MAXRECORD)
motif_printf(record, "NEVER");
else
motif_printf(record, "%d", hexagonsRecord[i][j]);
}
static int HandleSolved(counter, size, corners)
int counter, size, corners;
{
int i = (corners) ? 1 : 0, j = size - MINHEXAGONS;
if (size <= MAXHEXAGONS && counter < hexagonsRecord[i][j]) {
hexagonsRecord[i][j] = counter;
WriteRecords();
PrintRecord(size, corners);
return TRUE;
}
return FALSE;
}
static void ReadRecords()
{
FILE *fp;
int i, j, n;
for (i = 0; i < MAXORIENT; i++)
for (j = 0; j < MAXHEXAGONS - MINHEXAGONS + 1; j++)
hexagonsRecord[i][j] = MAXRECORD;
if ((fp = fopen(SCOREFILE, "r")) == NULL)
motif_printf(message, "Can not open %s, taking defaults.\n", SCOREFILE);
else {
for (i = 0; i < MAXORIENT; i++)
for (j = 0; j < MAXHEXAGONS - MINHEXAGONS + 1; j++) {
(void) fscanf(fp, "%d", &n);
hexagonsRecord[i][j] = n;
}
(void) fclose(fp);
}
}
static void WriteRecords()
{
FILE *fp;
int i, j;
if ((fp = fopen(SCOREFILE, "w")) == NULL)
motif_printf(message, "Can not write to %s.", SCOREFILE);
else {
for (i = 0; i < MAXORIENT; i++) {
for (j = 0; j < MAXHEXAGONS - MINHEXAGONS + 1; j++)
(void) fprintf(fp, "%d ", hexagonsRecord[i][j]);
(void) fprintf(fp, "\n");
}
(void) fclose(fp);
}
}
#include <varargs.h>
static void motif_printf(va_alist)
va_dcl
{
Widget w;
char *format;
va_list args;
char str[1000];
Arg wargs[10];
XmString xmstr;
va_start(args);
w = va_arg(args, Widget);
if (!XtIsSubclass(w, xmLabelWidgetClass))
XtError("xs_motif_printf() requires a Label Widget");
format = va_arg(args, char *);
(void) vsprintf(str, format, args);
xmstr = XmStringCreateLtoR(str, XmSTRING_DEFAULT_CHARSET);
XtSetArg(wargs[0], XmNlabelString, xmstr);
XtSetValues(w, wargs, 1);
va_end(args);
}